package pack

import (
	"mall_frontend_api/biz/model"
	"mall_frontend_api/biz/model/cart"
	"mall_frontend_api/biz/util"
	"strings"
)

func Cart(info *model.Cart, productInfo *model.Product, productItemList []*model.ProductItem, productSpecMap map[int64]*model.ProductSpec, productSpecValueMap map[int64]*model.ProductSpecValue) *cart.Cart {
	data := cart.NewCart()
	if info == nil {
		return data
	}
	data.ID = info.ID
	data.Number = info.Number
	if productInfo != nil {
		data.ProductID = productInfo.ID
		data.ProductImage = GetImage(productInfo.Image)
		data.ProductName = productInfo.Name
		var price float64
		var specList = make([]*cart.CartProductSpec, 0)
		for _, item := range productItemList {
			if item.SkuID == info.SkuID {
				price = item.SellPrice
				skuValueList := strings.Split(item.SkuValue, ":")
				var cartProductSpec = cart.NewCartProductSpec()
				for k, s := range skuValueList {
					if k%2 == 0 {
						cartProductSpec = cart.NewCartProductSpec()
					}
					vList := strings.Split(s, "-")
					if vList[0] == "s" {
						if name, ok := productSpecMap[util.String2Int64(vList[1], 0)]; ok {
							cartProductSpec.Name = name.Name
						}
					} else {
						if name, ok := productSpecValueMap[util.String2Int64(vList[1], 0)]; ok {
							cartProductSpec.Value = name.Name
						}
					}
					if cartProductSpec != nil && k%2 == 0 {
						specList = append(specList, cartProductSpec)
					}
				}

				break
			}
		}
		data.ProductPrice = price
		data.SpecList = specList
	}
	return data
}

func CartList(list []*model.Cart, productMap map[int64]*model.Product, productItemMap map[int64][]*model.ProductItem, productSpecMap map[int64]map[int64]*model.ProductSpec, productSpecValueMap map[int64]map[int64]*model.ProductSpecValue) []*cart.Cart {
	var data = make([]*cart.Cart, 0)
	if len(list) == 0 {
		return data
	}
	for _, c := range list {
		data = append(data, Cart(c, productMap[c.ProductID], productItemMap[c.ProductID], productSpecMap[c.ProductID], productSpecValueMap[c.ProductID]))
	}
	return data
}
